package com.happle.gcmclient.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.happle.gcmclient.R;
import com.happle.gcmclient.obj.ConversationObj;
import com.happle.gcmclient.obj.MessageObj;
import com.happle.gcmclient.utility.DateTimeUtility;

public class ConversionAdapter extends BaseAdapter {

	private String TAG = "ConversionAdapter.java";
	private Activity context;
	private List<ConversationObj> listConversationObj;
	private static LayoutInflater inflater = null;

	public ConversionAdapter(Activity activity,
			List<ConversationObj> listConversationObj) {
		context = activity;
		this.listConversationObj = listConversationObj;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return listConversationObj.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.conversation_row, null);

			holder.lblName = (TextView) convertView
					.findViewById(R.id.conversation_row_name);
			holder.lblDate = (TextView) convertView
					.findViewById(R.id.conversation_row_date);
			holder.lblCount = (TextView) convertView
					.findViewById(R.id.conversation_row_count);
			holder.lblStatus = (TextView) convertView
					.findViewById(R.id.conversation_row_status);
			holder.llWrapper = (LinearLayout) convertView
					.findViewById(R.id.conversation_row_wrapper);
            holder.imgCount = (ImageView) convertView.findViewById(R.id.conversation_row_count_image);
            holder.imgStatus = (ImageView) convertView.findViewById(R.id.conversation_closed_image);
			convertView.setTag(holder);
		}

		ConversationObj item = listConversationObj.get(position);

		if (item != null) {
			holder = (ViewHolder) convertView.getTag();
			holder.lblName.setText(item.get_message());
			holder.lblDate.setText(DateTimeUtility
					.reformatDateTimeWithPhoneSetting(context,
							item.get_datetime()));

			holder.lblCount.setText(item.get_msgs_count() + " "
					+ context.getString(R.string.message));
			if (item.is_conv_status() == false) {
                holder.llWrapper.setBackgroundResource(R.drawable.message_close_bg_style);
                holder.lblName.setTextColor(context.getResources().getColor(R.color.conversation_title_closed));
                holder.lblCount.setTextColor(context.getResources().getColor(R.color.conversation_title_closed));
                holder.lblDate.setTextColor(context.getResources().getColor(R.color.conversation_title_closed));
                holder.imgCount.setImageResource(R.drawable.message_num_icon_inactive);
				holder.lblStatus.setText(R.string.closedConversationLong);
				holder.lblStatus.setVisibility(View.VISIBLE);
                holder.imgStatus.setVisibility(View.VISIBLE);
			} else {
				holder.llWrapper.setBackgroundResource(R.drawable.message_open_bg_style);
                holder.lblName.setTextColor(context.getResources().getColor(R.color.conversation_title_open));
                holder.lblCount.setTextColor(context.getResources().getColor(R.color.conversation_label_open));
                holder.lblDate.setTextColor(context.getResources().getColor(R.color.conversation_label_open));
                holder.imgCount.setImageResource(R.drawable.message_num_icon_active);
				holder.lblStatus.setVisibility(View.GONE);
                holder.imgStatus.setVisibility(View.GONE);
			}

			if (item.is_has_new_messages()) {
				holder.lblCount.setTextColor(context.getResources().getColor(
						R.color._red));
			}

		} else {
			Log.i(TAG, "Null Object !");
		}
		return convertView;

	}

	class ViewHolder {
		TextView lblName;
		TextView lblDate;
		TextView lblCount;
		TextView lblStatus;
        ImageView imgCount;
        ImageView imgStatus;
		LinearLayout llWrapper;
	}
}
